package com.edu.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.demo.entity.UsersFriend;
import com.edu.demo.entity.UsersInfo;
import com.edu.demo.entity.dto.UserLoginDTO2;
import com.edu.demo.mapper.UsersFriendMapper;
import com.edu.demo.mapper.UsersInfoMapper;
import com.edu.demo.service.IUsersFriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 好友关系 服务实现类
 * </p>
 *
 * @author Ashe1y
 * @since 2024-03-13
 */
@Service
public class UsersFriendServiceImpl extends ServiceImpl<UsersFriendMapper, UsersFriend> implements IUsersFriendService {

    @Autowired
    private UsersFriendMapper usersFriendMapper;
    @Autowired
    private UsersInfoMapper usersInfoMapper;

    @Override
    public void addFriend(UsersFriend usersFriend) {
        usersFriendMapper.insert(usersFriend);
    }

    @Override
    public List<UserLoginDTO2> findFriend(int userId) {
        ArrayList<UserLoginDTO2> userLoginDTO2s = new ArrayList<>();

        List<UsersFriend> usersFriends = usersFriendMapper.selectByFriendIdAndStatus(userId, 0);
        for (UsersFriend usersFriend : usersFriends) {
            UserLoginDTO2 userLoginDTO2 = new UserLoginDTO2();
            UsersInfo usersInfo = usersInfoMapper.selectById(usersFriend.getUserId());
            BeanUtil.copyProperties(usersInfo, userLoginDTO2);
            userLoginDTO2.setPhone(DesensitizedUtil.mobilePhone(userLoginDTO2.getPhone()));
            userLoginDTO2.setIdCard(DesensitizedUtil.idCardNum(userLoginDTO2.getIdCard(), 3, 4));
            userLoginDTO2s.add(userLoginDTO2);
        }
        return userLoginDTO2s;
    }

    @Override
    public List<UserLoginDTO2> findFriendList(int userId) {
        ArrayList<UserLoginDTO2> userLoginDTO2s = new ArrayList<>();

        List<UsersFriend> usersFriends = usersFriendMapper.selectByFriendIdAndStatus(userId, 1);
        for (UsersFriend usersFriend1 : usersFriends) {
            UserLoginDTO2 userLoginDTO2 = new UserLoginDTO2();
            UsersInfo usersInfo = usersInfoMapper.selectById(usersFriend1.getUserId());
            BeanUtil.copyProperties(usersInfo, userLoginDTO2);
            userLoginDTO2.setPhone(DesensitizedUtil.mobilePhone(userLoginDTO2.getPhone()));
            userLoginDTO2.setIdCard(DesensitizedUtil.idCardNum(userLoginDTO2.getIdCard(), 3, 4));
            userLoginDTO2s.add(userLoginDTO2);
        }
        List<UsersFriend> usersFriends1 = usersFriendMapper.selectByUserIdAndStatus(userId, 1);
        for (UsersFriend friend : usersFriends1) {
            UserLoginDTO2 userLoginDTO2 = new UserLoginDTO2();
            UsersInfo usersInfo = usersInfoMapper.selectById(friend.getUserId());
            BeanUtil.copyProperties(usersInfo, userLoginDTO2);
            userLoginDTO2.setPhone(DesensitizedUtil.mobilePhone(userLoginDTO2.getPhone()));
            userLoginDTO2.setIdCard(DesensitizedUtil.idCardNum(userLoginDTO2.getIdCard(), 3, 4));
            userLoginDTO2s.add(userLoginDTO2);
        }
        return userLoginDTO2s;
    }

    @Override
    public void friendOperate(UsersFriend usersFriend) {
        int status = usersFriend.getStatus();
        usersFriend.setStatus(0);
        UsersFriend friend = usersFriendMapper.selectByFriendIdAndUserIdAndStatus(usersFriend);
        usersFriend.setStatus(status);
        usersFriendMapper.updateStatusById(friend.getId(), usersFriend.getStatus());
    }
}
